//
//  LancamentoController.m
//  ApoioLivre
//
//  Created by Tarek Abdala on 13/11/10.
//  Copyright 2010 UFSC - Universidade Federal Santa Catarina. All rights reserved.
//

#import "LancamentoController.h"
#import "JSON.h"
@implementation LancamentoController

@synthesize tableViewLancamento;
@synthesize responseData;


- (void)viewDidLoad {
	[super viewDidLoad];
	
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
	self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
	NSString *myRowJson = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://apoiolivreapp.appspot.com/data/BuscaMovimento"]];
	
    // step 2: parse json into our 'vehicles' string
	NSError *error;
	SBJsonParser *json = [[SBJsonParser new] autorelease];
	NSArray *vehicles = [json objectWithString:myRowJson error:&error];
	//[responseString release];       
    
	tableViewLancamento = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStyleGrouped];
	tableViewLancamento.delegate = self;
	tableViewLancamento.dataSource = self;
	tableViewLancamento.autoresizesSubviews = YES;
	
	self.view = tableViewLancamento;
	
    // step 3: print either error or result
	if(vehicles == nil) 
    {
        NSString *errorString = [NSString stringWithFormat:@"JSON parsing failed: %@", [error localizedDescription]];
        NSLog(@"ERROR: %@", errorString);
    }
	else 
    {
		NSMutableString *text = [[NSMutableString alloc] init];    
		
		for (int i = 0; i < [vehicles count]; i++){
			[text appendFormat:@"%@\n", [vehicles objectAtIndex:i]];
		}
		
		text = [text stringByReplacingOccurrencesOfString:@"\"" withString:@""];
		text = [text stringByReplacingOccurrencesOfString:@"{" withString:@""];
		text = [text stringByReplacingOccurrencesOfString:@"}" withString:@""];
		text = [text stringByReplacingOccurrencesOfString:@"description =" withString:@"Desc:"];
		text = [text stringByReplacingOccurrencesOfString:@"value =" withString:@"Valor:"];
		
		NSArray *words = [text componentsSeparatedByString:@";"];
		
		responseData = [[NSMutableArray alloc] initWithArray:words];
		
        NSLog(@"%@", text); 
    }	
}

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
	switch (section) {
		case 0:
			return [responseData count];
		case 1:
			return 0;			
	}
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    
	if(indexPath.section == 0)
		[cell setText:[responseData objectAtIndex:indexPath.row]];
	//else 
	//	[cell setText:@"Estou na secao 2"];
	
	cell.textColor = [UIColor blueColor];
    return cell;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc {
    [super dealloc];
}

@end